//
//  NSDate+XMNYR.m
//  XMKit
//
//  Created by chenming on 2017/12/21.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#import "NSDate+XMNYR.h"

@implementation NSDate (XMNYR)


- (NSInteger)year {
    NSDateComponents *components = [NSDateComponents xm_dateComponentsFromDate:self];
    return components.year;
}

- (NSInteger)month {
    NSDateComponents *components =  [NSDateComponents xm_dateComponentsFromDate:self];
    return components.month;
}

- (NSInteger)day {
    NSDateComponents *components =  [NSDateComponents xm_dateComponentsFromDate:self];
    return components.day;
}

- (NSInteger)hour {
    NSDateComponents *components =  [NSDateComponents xm_dateComponentsFromDate:self];
    return components.hour;
}

- (NSInteger)minute {
    NSDateComponents *components = [NSDateComponents xm_dateComponentsFromDate:self];
    return components.minute;
}

- (NSInteger)seconds {
    NSDateComponents *components =  [NSDateComponents xm_dateComponentsFromDate:self];
    return components.second;
}

- (NSInteger)weekday {
    NSDateComponents *components =  [NSDateComponents xm_dateComponentsFromDate:self];
    return components.weekday;
}

+ (NSDate *)xm_dateWithDateString:(NSString *)dateString {
    NSDate *date = nil;
    date = [self xm_dateWithFormat_yyyy_MM_dd_HH_mm_ss_string:dateString];
    if(date) return date;
    date = [self xm_dateWithFormat_yyyy_MM_dd_HH_mm_string:dateString];
    if(date) return date;
    date = [self xm_dateWithFormat_yyyy_MM_dd_HH_string:dateString];
    if(date) return date;
    date = [self xm_dateWithFormat_yyyy_MM_dd_string:dateString];
    if(date) return date;
    date = [self xm_dateWithFormat_yyyy_MM_string:dateString];
    if(date) return date;
    return nil;
}

+ (NSDate *)xm_dateWithFormat_yyyy_MM_dd_HH_mm_ss_string:(NSString *)string {
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}

+ (NSDate *)xm_dateWithFormat_yyyy_MM_dd_HH_mm_string:(NSString *)string {
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}

+ (NSDate *)xm_dateWithFormat_yyyy_MM_dd_HH_string:(NSString *)string {
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH"];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}

+ (NSDate *)xm_dateWithFormat_yyyy_MM_dd_string:(NSString *)string {
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}

+ (NSDate *)xm_dateWithFormat_yyyy_MM_string:(NSString *)string {
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM"];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}

@end
